package com.cyl.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.wms.domain.Container;
import com.cyl.wms.domain.Loader;
import com.cyl.wms.mapper.ContainerMapper;

import com.cyl.wms.pojo.query.ContainerQuery;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 地址码Service业务层处理
 *
 * @author Lisi
 */
@Service
public class ContainerService {
    @Autowired
    private ContainerMapper containerMapper;

    /**
     * 查询货区
     *
     * @param id 货区主键
     * @return 货区
     */
    public Container selectById(Long id) {
        return containerMapper.selectById(id);
    }

    /**
     * 查询货区列表
     *
     * @param query 查询条件
     * @param page  分页条件
     * @return 货区
     */
    public List<Container> selectList(ContainerQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<Container> qw = new QueryWrapper<>();
        qw.eq("del_flag", 0);
        String containerNo = query.getContainerNo();
        if (!StringUtils.isEmpty(containerNo)) {
            qw.eq("container_no", containerNo);
        }
        String containerNameLike = query.getContainerNameLike();
        if (!StringUtils.isEmpty(containerNameLike)) {
            qw.like("container_name", containerNameLike);
        }
        Long warehouseId = query.getWarehouseId();
        if (warehouseId != null) {
            qw.eq("warehouse_id", warehouseId);
        }
        return containerMapper.selectList(qw);
    }

    /**
     * 新增货区
     *
     * @param Container 货区
     * @return 结果
     */
    public int insert(Container Container) {
        Container.setDelFlag(0);
        Container.setCreateTime(LocalDateTime.now());
        return containerMapper.insert(Container);
    }

    /**
     * 修改货区
     *
     * @param area 货区
     * @return 结果
     */
    public int update(Container area) {
        return containerMapper.updateById(area);
    }

    /**
     * 批量删除货区
     *
     * @param ids 需要删除的货区主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return containerMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除货区信息
     *
     * @param id 货区主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return containerMapper.updateDelFlagByIds(ids);
    }

    public List<Container> selectByIdIn(Collection<Long> ids) {
        QueryWrapper<Container> qw = new QueryWrapper<>();
        qw.in("id", ids);
        return containerMapper.selectList(qw);
    }

    public Container createContainerByLoader(Loader loader) {
        Container container = this.containerMapper.findOneByLoaderId(loader.getId());
        if(container == null){
            container = new Container();
            container.setContainerNo(loader.getLoaderNo());
            container.setContainerName(loader.getLoaderName());
            container.setDelFlag(0);
            container.setCreateTime(LocalDateTime.now());
            container.setRemark("loader:"+loader.getLoaderName());
            container.setWarehouseId(loader.getWarehouseId());
            this.containerMapper.insert(container);
            return container;
        }
        return container;
    }
}
